package cn.ac.cintcm.prespring.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.ac.cintcm.prespring.util.Config;

/**
 * 树在内存中的组织
 * @author 何前锋 Franklin He(franklinheqf@gmail.com)
 *
 */
public class VisitMemory implements Visit, Comparable {
    //以此为中心，组织加减树
    private int root = -1;
    //保存树的结构
    private HashMap<Integer, HashMap<Integer, List<MemoryNode>>> ladder =
        new HashMap<Integer, HashMap<Integer, List<MemoryNode>>>();
    
    public VisitMemory(int id) {
        this.root = id;
    }

    public void visit(int parent, int countIndex, int herb, int resultPrescription) {
        HashMap<Integer, List<MemoryNode>> rsMap = null;

        rsMap = ladder.get(countIndex);
        if (rsMap == null) {
            rsMap = new HashMap<Integer, List<MemoryNode>>();
            ladder.put(countIndex, rsMap);
        }
        List<MemoryNode> nodes = rsMap.get(herb);
        if (nodes == null) {
            nodes = new ArrayList<MemoryNode>();
            rsMap.put(herb, nodes);
        }
        MemoryNode node = new MemoryNode();
        node.setParent(parent);
        node.setChild(resultPrescription);
        nodes.add(node);
    }

    public int getRoot() {
        return root;
    }

    public void setRoot(int id) {
        this.root = id;
    }

    public HashMap<Integer, HashMap<Integer, List<MemoryNode>>> getLadder() {
        return ladder;
    }

    public void setLadder(
            HashMap<Integer, HashMap<Integer, List<MemoryNode>>> ladder) {
        this.ladder = ladder;
    }

    public int compareTo(Object sp) {
        int compare = 1;
        if (sp != null && sp instanceof VisitMemory) {
            VisitMemory o = (VisitMemory) sp;
            compare = o.getLength() - this.getLength() ;
        }
        return compare;
    }

    /**
     * 树形结构的层数
     * @return
     */
    public int getLength() {
        int length = 0;
        if (ladder != null) {
            length = ladder.size();
        }
        return length;
    }
    
    /**
     * 打印内存树结构，格式：
     * 父节点编号:药味数:加减药物编号:子节点编号
     */
    public String toString() {
        StringBuilder sb = new StringBuilder();
        HashMap<Integer, List<MemoryNode>> rsMap = null;
        //组成个数
        for (Integer i : ladder.keySet()) {
            rsMap = ladder.get(i);
            if (rsMap != null) {
                //不同的组成元素
                for (Integer h : rsMap.keySet()) {
                    //不同的组成节点
                    List<MemoryNode> ps = rsMap.get(h);
                    if (ps != null) {
                        for (MemoryNode p : ps) {
                            if (p != null) {
                                sb.append(p.getParent());
                                sb.append(Config.FIELD_SPLITTER);
                                sb.append(i);
                                sb.append(Config.FIELD_SPLITTER);
                                sb.append(h);
                                sb.append(Config.FIELD_SPLITTER);
                                sb.append(p.getChild());
                                sb.append(Config.NEW_LINE);
                            }
                        }
                    }
                }
            }
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length()-1);
        }
        return sb.toString();
    }

    public Set<Integer> getLadderId() {
        Set<Integer> set = new HashSet<Integer>();
        HashMap<Integer, List<MemoryNode>> rsMap = null;
        //组成个数
        for (Integer i : ladder.keySet()) {
            rsMap = ladder.get(i);
            if (rsMap != null) {
                //不同的组成元素
                for (Integer h : rsMap.keySet()) {
                    //不同的组成节点
                    List<MemoryNode> ps = rsMap.get(h);
                    if (ps != null) {
                        for (MemoryNode p : ps) {
                            if (p != null) {
                                set.add(p.getChild());
                            }
                        }
                    }
                }
            }
        }
        return set;
    }
    
    public Set<Integer> getLadderElementId() {
        Set<Integer> set = new HashSet<Integer>();
        HashMap<Integer, List<MemoryNode>> rsMap = null;
        //组成个数
        for (Integer i : ladder.keySet()) {
            rsMap = ladder.get(i);
            if (rsMap != null) {
                //不同的组成元素
                for (Integer h : rsMap.keySet()) {
                    set.add(h);
                }
            }
        }
        return set;
    }

}
